import GameManager, { GameState } from "./GameManager";
import TipPanel from "../UI/TipPanel";
import AudioManager from "./AudioManager";
import Cell from "../Core/Cell";
import CellFunctions from "../Core/CellFunctions";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchManager extends cc.Component {
    public static Instance: TouchManager;

    startPos: cc.Vec3;
    currentPos: cc.Vec3;
    endPos: cc.Vec3;

    force:number;
    hasMove:boolean = false;
    isPress:boolean = false;

    horizontalTouchDistance = 40;
    verticalTouchDistance = 100;

    onLoad() {
        TouchManager.Instance = this;
    }

    start() {
        this.registerEvent();
    }

    registerEvent() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onEventStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onEventMove, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onEventCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onEventEnd, this);
    }

    Move(x:number)
    {
        this.hasMove = true;
        if (x < 0)
        {
            CellFunctions.MoveLefOrRight(-1);
        }
        else if (x > 0)
        {
            CellFunctions.MoveLefOrRight(+1);
        }
    }

    //触摸开始
    onEventStart(event) {
        //世界坐标
        this.startPos = event.getLocation();
        this.currentPos = event.getLocation();
        this.isPress = true;
        this.force = -0.4;
        // console.log('start Event \n worldPoint=', this.startPos);
    }

    // 触摸移动
    onEventMove(event) {
        //世界坐标
        this.currentPos = event.getLocation();
        let diffX:number = this.currentPos.x - this.startPos.x;
        if (Math.abs(diffX) > this.horizontalTouchDistance)
        {
            if (this.hasMove == false)
            {
                this.Move(diffX);
            }
        }
    }

    update(dt)
    {
        if (this.isPress == true) {
            let diffX:number = this.currentPos.x - this.startPos.x;
            if (Math.abs(diffX) > this.horizontalTouchDistance)
            {
                this.force += dt;
                if (this.force >= 0.1)
                {
                    this.force = 0;
                    this.Move(diffX);
                }
            }
        }
    }

    /**
     * 触摸
     * 当手指在目标节点区域外离开屏幕时
     * 比如说，触摸node的size是200x200。
     * 当超过这个区域时，就是触发这个事件
     * @param {*} event 
     */
    onEventCancel(event) {
        this.endPos = event.getLocation();

        let diffY:number = this.endPos.y - this.startPos.y;
        if (this.hasMove == false)
        {
            if (diffY < -this.verticalTouchDistance)
            {
                //TipPanel.Instance.Open("攻击");
                CellFunctions.ControlMoveDown();
            }
        }

        this.hasMove = false;
        this.isPress = false;
    }

    //当手指在目标节点区域内离开屏幕时
    onEventEnd(event) {
        this.endPos = event.getLocation();

        let diffY:number = this.endPos.y - this.startPos.y;
        if (this.hasMove == false)
        {
            if (diffY < -this.verticalTouchDistance)
            {
                //TipPanel.Instance.Open("攻击");
                CellFunctions.ControlMoveDown();
            }
        }

        this.hasMove = false;
        this.isPress = false;
    }


}
